/*
 *   A task scheduler that implements Monte-Carlo simulation
 *
 *   Copyright (C) 2011 Systasis Computer Systems, Inc.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *   $Id: HelpView.java 86 2011-08-08 22:29:50Z jchimene@gmail.com $
 */
package com.systasis.sked.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.systasis.sked.client.presenter.HelpPresenter;
import com.systasis.sked.client.resources.SkedConstants;
import com.systasis.sked.client.resources.SkedStrings;

public class HelpView extends LazyPanel implements HelpPresenter.MyView {

    final private SkedConstants CONSTANTS = GWT.create(SkedConstants.class);

    private Element currentTopic = null;
    private PopupPanel helpPanel = null;
    final private SkedStrings helpStrings = GWT.create(SkedStrings.class);
    final private HTML helpText = new HTML(CONSTANTS.nbsp());

    @Override
    public void addToSlot(Object slot, Widget content) {
    }

    @Override
    public Widget asWidget() {
	return null;
    }

    @Override
    public void helpShow() {
	ensureWidget();
	if (null != helpPanel) {
	    helpPanel.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
		@Override
		public void setPosition(int offsetWidth, int offsetHeight) {
		    final int left = (Window.getClientWidth() - offsetWidth) / 3;
		    final int top = Math.abs((Window.getClientHeight() - offsetHeight)) / 10;
		    helpPanel.setPopupPosition(left, top);
		    if (null == currentTopic) {
			currentTopic = DOM.getElementById(CONSTANTS.helpDefaultTopic());
		    }
		}
	    });
	}
    }

    public native void jsHooks(HelpView h) /*-{
					   $wnd.reveal = function(theTopic) {
					   h.@com.systasis.sked.client.view.HelpView::revealHelpTopic(Ljava/lang/String;)(theTopic);
					   }
					   }-*/;

    @Override
    public void removeFromSlot(Object slot, Widget content) {
    }

    @Override
    public void setContent(String theContent) {
	helpText.setHTML(theContent);
    }

    @Override
    public void setInSlot(Object slot, Widget content) {
    }

    private final void revealHelpTopic(final String theTopic) {
	final Element e = DOM.getElementById(theTopic);
	if (null != currentTopic) {
	    currentTopic.setClassName(helpStrings.hidden());
	}
	e.setClassName(helpStrings.visible());
	currentTopic = e;
    }

    @Override
    protected Widget createWidget() {
	if (null == helpPanel) {
	    final VerticalPanel helpWidget = new VerticalPanel();
	    helpPanel = new PopupPanel(false, false);

	    helpWidget.add(new Button(helpStrings.closeButtonText(), new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {
		    helpPanel.hide();
		}
	    }));
	    helpWidget.add(helpText);

	    helpPanel.setTitle(helpStrings.titleHelp());
	    helpPanel.setAnimationEnabled(true);
	    helpPanel.setWidget(helpWidget);
	}
	return helpPanel;
    }
}
